package com.lineage.server.timecontroller.pc;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_Disconnect;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.PcOtherThreadPool;
import com.lineage.server.world.World;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PcDeleteTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(PcDeleteTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 1100;
    this._timer = PcOtherThreadPool.get().scheduleAtFixedRate(this, 1100L, 1100L);
  }
  
  public void run() {
    try {
      Collection<L1PcInstance> all = World.get().getAllPlayers();
      if (all.isEmpty())
        return; 
      Iterator<L1PcInstance> iter = all.iterator();
      while (iter.hasNext()) {
        L1PcInstance tgpc = iter.next();
        if (!tgpc.isDead())
          continue; 
        if (tgpc.get_delete_time() <= 0)
          continue; 
        int newtime = tgpc.get_delete_time() - 1;
        tgpc.set_delete_time(newtime);
        if (tgpc.get_delete_time() > 0)
          continue; 
        tgpc.sendPackets((ServerBasePacket)new S_Disconnect());
      } 
    } catch (Exception e) {
      _log.error("PC 死亡删除处理时间轴异常重启", e);
      PcOtherThreadPool.get().cancel(this._timer, false);
      PcDeleteTimer pcHprTimer = new PcDeleteTimer();
      pcHprTimer.start();
    } 
  }
}
